import axios from 'axios'
import { ElMessage } from 'element-plus'
//引入仓库
import { store } from '@/stores/modules/user/user'

const request = axios.create({
    baseURL: '',
    timeout: 5000
})
//情求拦截器
request.interceptors.request.use((config)=>{
    //设置请求头
    let userstore = store()
    if(userstore.token){
        config.headers.Authorization = userstore.token
    }
    return config
})


//响应拦截器
request.interceptors.response.use((response)=>{
    //成功后的回调
    return response.data;
},(error)=>{
    console.log(123)
    //失败的回调
    let message = '';
    //响应状态码
    let status = error.response.status
    switch(status){
        case 401:
            message = 'Token过期,请重新登录'
            break;
        case 404:
            message = '请求资源不存在'
            break;
        case 500:
            message = "服务器错误，请稍后"
            break
        default:
            message = "未知错误，请稍后重试"
            break; 
    }
    ElMessage({
        type:'error',
        message
    });
    return Promise.reject(error)
})
export default request
